
options(scipen=20)

library(MASS)
library(WhatIf)
library(RColorBrewer)
library(simcf)
library(verification)
library(foreign)
library(nnet)
library(xtable)
library(apsrtable)
library(ggplot2)
library(dplyr)
library(stargazer)
col <- brewer.pal(3,"Dark2")

anes<-read.dta("anes.dta")
anes<- anes %>% mutate(ai=protest+petition+don_rel+don_soc+community+meet+volunteer+official)

ncps<-read.dta("ncps.dta")
ncps<-ncps %>% mutate(ai=petition+community+meeting+org+letter+donate+dem+elect)

# Alternative specifications for the appendix #

# discrimination in the ANES should be modeled using ordered logit, not OLS #

anes$disc <- factor(anes$disc)
m4<-polr(disc~prox2+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent+nonwhite, 
         method="probit", data=anes,weights=weight)

wanes<-anes [ which(anes$nonwhite==0), ]
wanes$disc <- factor(wanes$disc)
m5<-polr(disc~prox2+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent,
         method= "probit", data=wanes,weights=weight)

nwanes<-anes[ which(anes$nonwhite==1), ]
nwanes$disc <- factor(nwanes$disc)
m6<-polr(disc~prox2+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent,
         method="probit",data=nwanes,weights=weight)

stargazer(m4,m5,m6, no.space=TRUE,
          single.row = FALSE,
          font.size = "footnotesize",column.sep.width="1.5pt",
          title="The impact of contact on percieved discrimination in the ANES: Ordered Probit Model",
          covariate.labels=c("Proximal",
                             "Personal",
                             "Efficacy",
                             "Female","Age: 18-34","Age: 65+",
                             "Education","20k-40k","40k-60k","60k-80k","80k-100k","100k+",
                             "Missing Inc","Political Int","Religiosity",
                             "Republican","Independent","Mode","Nonwhite",
                             "Constant"),
          model.numbers=FALSE,
          star.cutoffs = c(0.05, 0.01, 0.001),
          column.labels = c("Total","White", "Nonwhite"),
          dep.var.labels.include = FALSE,
          out = "anes_disc_olog.tex")

##############################################################################################################
######## DISCRIMINATION/INJUSTICE ON PARTICIPATION, STRAIGHT MODELS, should be modeled using poisson #########
##############################################################################################################

##########
## NCPS ##
##########

m1<-glm(ai~prox2+pers+cpi+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent+mode+nonwhite,
        family="poisson",data=ncps,weights=weight)

wncps<-ncps[ which(ncps$nonwhite==0), ]

m2<-glm(ai~prox2+pers+cpi+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent+mode, 
       family="poisson",data=wncps)

nwncps<-ncps[ which(ncps$nonwhite==1), ]

m3<-glm(ai~prox2+pers+cpi+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent+mode, 
       family="poisson",data=nwncps)

##########
## ANES ##
##########

anes<-read.dta("/Users/hannah/Dropbox/my projects/dissertation/CH 3 Article Version/ANES 2016/anes_timeseries_2016_dta/anessmall.dta")
anes<- anes %>% mutate(ai=protest+petition+don_rel+don_soc+community+meet+volunteer+official)


m4<-glm(ai~prox2+pers+disc+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent+nonwhite, 
       family="poisson",data=anes,weights=weight)

wanes<-anes [ which(anes$nonwhite==0), ]

m5<-glm(ai~prox2+pers+disc+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent, 
       family="poisson",data=wanes,weights=weight)

nwanes<-anes[ which(anes$nonwhite==1), ]

m6<-glm(ai~prox2+pers+disc+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent, 
       family="poisson",data=nwanes,weights=weight)

stargazer(m1,m2,m3, no.space=TRUE,
          single.row = FALSE,
          font.size = "footnotesize",column.sep.width="1.5pt",
          title="The impact of contact and injustice on participation in the NCPS: Poisson Model",
          covariate.labels=c("Proximal",
                             "Personal",
                             "Injustice",
                             "Efficacy",
                             "Female","Age: 18-34","Age: 65+",
                             "Education","20k-40k","40k-60k","60k-80k","80k-100k","100k+",
                             "Missing Inc","Political Int","Religiosity",
                             "Republican","Independent","Mode","Nonwhite",
                             "Constant"),
          dep.var.labels.include = FALSE,
          model.numbers=FALSE,
          star.cutoffs = c(0.05, 0.01, 0.001),
          column.labels = c("Total","White", "Nonwhite"),
          out = "ncps_part_poisson.tex",
          dep.var.labels   = (""))

stargazer(m4,m5,m6, no.space=TRUE,
          single.row = FALSE,
          font.size = "footnotesize",column.sep.width="1.5pt",
          title="The impact of contact and injustice on participation in the ANES: Poisson Model",
          covariate.labels=c("Proximal",
                             "Personal",
                             "Discrimination",
                             "Efficacy",
                             "Female","Age: 18-34","Age: 65+",
                             "Education","20k-40k","40k-60k","60k-80k","80k-100k","100k+",
                             "Missing Inc","Political Int","Religiosity",
                             "Republican","Independent","Nonwhite",
                             "Constant"),
          dep.var.labels.include = FALSE,
          model.numbers=FALSE,
          star.cutoffs = c(0.05, 0.01, 0.001),
          column.labels = c("Total","White", "Nonwhite"),
          out = "anes_part_poisson.tex",
          dep.var.labels   = (""))


##############################################################################################################
######## DISCRIMINATION/INJUSTICE ON PARTICIPATION, INTERACTION MODELS, should be modeled using poisson #########
##############################################################################################################

m1<-glm(ai~prox2*cpi+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent+mode+nonwhite, 
       family="poisson",data=ncps,weights=weight)

wncps<-ncps[ which(ncps$nonwhite==0), ]

m2<-glm(ai~prox2*cpi+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent+mode, 
       family="poisson",data=wncps)

nwncps<-ncps[ which(ncps$nonwhite==1), ]

m3<-glm(ai~prox2*cpi+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent+mode, 
       family="poisson",data=nwncps)

##########
## ANES ##
##########

m4<-glm(ai~prox2*disc+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent+nonwhite, 
       family="poisson",data=anes,weights=weight)

wanes<-anes [ which(anes$nonwhite==0), ]

m5<-glm(ai~prox2*disc+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent, 
       family="poisson",data=wanes,weights=weight)

nwanes<-anes[ which(anes$nonwhite==1), ]

m6<-glm(ai~prox2*disc+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+inc101+
         incmis+polint+church+republican+independent, 
       family="poisson",data=nwanes,weights=weight)
summary(m1)
stargazer(m1,m2,m3, no.space=TRUE,
          single.row = FALSE,
          font.size = "footnotesize",column.sep.width="1.5pt",
          title="The interactive effect of contact and injustice on participation in the NCPS: Poisson Model",
          order=c(1,2,3,21,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,22),
          covariate.labels=c("Proximal",
                             "Injustice",
                             "Prox*Injustice",
                             "Personal",
                             "Efficacy",
                             "Female","Age: 18-34","Age: 65+",
                             "Education","20k-40k","40k-60k","60k-80k","80k-100k","100k+",
                             "Missing Inc","Political Int","Religiosity",
                             "Republican","Independent","Mode","Nonwhite",
                             "Constant"),
          dep.var.labels.include = FALSE,
          model.numbers=FALSE,
          star.cutoffs = c(0.05, 0.01, 0.001),
          column.labels = c("Total","White", "Nonwhite"),
          out = "ncps_interact_poisson.tex",
          dep.var.labels   = (""))


stargazer(m4,m5,m6, no.space=TRUE,
          single.row = FALSE,
          font.size = "footnotesize",column.sep.width="1.5pt",
          title="The interactive effect of contact and injustice on participation in the ANES: Poisson Model",
          order=c(1,2,3,20,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,21),
          covariate.labels=c("Proximal",
                             "Personal",
                             "Discrimination",
                             "Prox*Discrimination",
                             "Personal",
                             "Efficacy",
                             "Female","Age: 18-34","Age: 65+",
                             "Education","20k-40k","40k-60k","60k-80k","80k-100k","100k+",
                             "Missing Inc","Political Int","Religiosity",
                             "Republican","Independent","Nonwhite",
                             "Constant"),
          dep.var.labels.include = FALSE,
          model.numbers=FALSE,
          star.cutoffs = c(0.05, 0.01, 0.001),
          column.labels = c("Total","White", "Nonwhite"),
          out = "anes_interact_poisson.tex",
          dep.var.labels   = (""))

####################################################################################
################### MEDIATION ANALYSIS W/ POISSON INSTEAD OF OLS ###################
####################################################################################

# NCPS 
model.m<-lm(cpi~prox2+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent+mode+nonwhite, 
            data=ncps,weights=weight)
model.y<-glm(ai~prox2+pers+cpi+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent+mode+nonwhite, 
            family="poisson",data=ncps, weights=weight)
out.1<-mediate(model.m, model.y, sims=5000, treat="prox2", mediator="cpi")
summary(out.1)

# white
model.m<-lm(cpi~prox2+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent+mode, 
            data=wncps)
model.y<-glm(ai~prox2+pers+cpi+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent+mode, 
            family="poisson",data=wncps)
out.2<-mediate(model.m, model.y, sims=5000, treat="prox2", mediator="cpi")
summary(out.2)

# nonwhite
model.m<-lm(cpi~prox2+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent+mode, 
            data=nwncps)
model.y<-glm(ai~prox2+pers+cpi+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent+mode, 
            family="poisson",data=nwncps)
out.3<-mediate(model.m, model.y, sims=5000, treat="prox2", mediator="cpi")
summary(out.3)

############################
######### ANES #############
############################

# total pop 
model.m<-polr(disc~prox2+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent+nonwhite, Hess = TRUE,
            data=anes,weights=weight)
model.y<-glm(ai~prox2+pers+disc+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent+nonwhite, 
            family="poisson",data=anes, weights=weight)
out.4<-mediate(model.m, model.y, sims=5000,treat="prox2", mediator="disc")
summary(out.4)

# white
model.m<-polr(disc~prox2+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent, Hess=TRUE,
            data=wanes,weights=weight)
model.y<-glm(ai~prox2+pers+disc+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent, 
            family="poisson",data=wanes, weights=weight)
out.5<-mediate(model.m, model.y, sims=5000,treat="prox2", mediator="disc")
summary(out.5)

# nonwhite
model.m<-polr(disc~prox2+pers+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent, Hess=TRUE,
            data=nwanes,weights=weight)
model.y<-glm(ai~prox2+pers+disc+inteff+female+young+old+edu4+inc40+inc60+inc80+inc100+
              inc101+incmis+polint+church+republican+independent, 
            family="poisson",data=nwanes, weights=weight)
out.6<-mediate(model.m, model.y, sims=5000,treat="prox2", mediator="disc")
summary(out.6)












